package fr.kirin.logger.model.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeSelectionModel;

import org.jdesktop.swingx.JXHeader;

import fr.kirin.logger.filter.FilterFactory;
import fr.kirin.logger.i18n.I18N;
import fr.kirin.logger.model.DefaultLevel;
import fr.kirin.logger.model.LoggerTreeModel;
import fr.kirin.logger.model.DefaultLoggerTreeModel.InfoBean;


/**
 * 
 *Main class of the GUI 
 * @author kirin
 *
 */
public class GraphicLoggerGUI
{

    private  JFrame mainFrame;
    private LoggerTreeModel model;


    public void setLocationRelativeTo(Component c)
    {
        mainFrame.setLocationRelativeTo(c);
    }


    public void setLocation(Point p)
    {
        mainFrame.setLocation(p);
    }


    /**
     * 
     *build a graphic logger with a model. 
     * @param model
     */
    public GraphicLoggerGUI(LoggerTreeModel model){
        this.model = model;

    }


    public void show(){
        if(mainFrame == null)
            init();

        mainFrame.setVisible(true);
    }

    public void hide(){
        if(mainFrame != null)
            mainFrame.setVisible(false);
    }



    public void addComponentListener(ComponentListener l)
    {
        if(mainFrame == null)
            init();
        mainFrame.addComponentListener(l);
    }
    
    private JMenuBar createMenuBar(){
    	JMenuBar bar = new JMenuBar();
    	
    	JMenu file = new JMenu(I18N.getMessage("file"));
    	JMenuItem close = new JMenuItem(I18N.getMessage("close"));
    	close.addActionListener(new ActionListener() {
		
			public void actionPerformed(ActionEvent e) {
				mainFrame.setVisible(false);
			}
		
		});
    	file.add(close);
    	
    	
    	JMenu option = new JMenu(I18N.getMessage("option"));
    	final JCheckBoxMenuItem always = new JCheckBoxMenuItem(I18N.getMessage("alwaysOnTop"));
    	always.addActionListener(new ActionListener() {
		
			public void actionPerformed(ActionEvent e) {
				if(always.isSelected())
					mainFrame.setAlwaysOnTop(true);
				else
					mainFrame.setAlwaysOnTop(false);
		
			}
		
		});
    	final ConfigurationManager confManager = new ConfigurationManager(mainFrame);
    	JMenuItem configuration = new JMenuItem(I18N.getMessage("configuration"));
    	configuration.addActionListener(new ActionListener() {
		
			public void actionPerformed(ActionEvent e) {
				confManager.getUIComponent().setVisible(true);
		
			}
		
		});
    	option.add(always);
    	option.add(configuration);
    	
    	
    	JMenu help = new JMenu(I18N.getMessage("?"));
    	JMenuItem helpTopic = new JMenuItem(I18N.getMessage("help"));
    	JMenuItem about = new JMenuItem(I18N.getMessage("about"));
    	help.add(helpTopic);
    	help.add(about);
    	
    	
    	
    	bar.add(file);
    	bar.add(option);
    	bar.add(help);
    	
    	
    	return bar;
    }


    private void init()
    {
        mainFrame = new JFrame("GraphicLogger4j");
        //XXX ajout de la menu bar ici
        //mainFrame.setJMenuBar(createMenuBar());
       
        //mainFrame.setLayout(new B;

        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        split.setContinuousLayout(true);
        split.setDividerSize(8);
        split.setOneTouchExpandable(true);

        TreeDisplayer tree = new TreeDisplayer(model);
        tree.setCellRenderer(new LoggerCellRenderer());
       
        tree.setPreferredSize(new Dimension(320,250));
        tree.addFilter(FilterFactory.createLevelFilterGUI(DefaultLevel.INFO,"/resources/greenb.gif","hideInfo"));
        tree.addFilter(FilterFactory.createLevelFilterGUI(DefaultLevel.WARNING,"/resources/orangeb.gif","hideWarning"));
        tree.addFilter(FilterFactory.createLevelFilterGUI(DefaultLevel.ERROR,"/resources/redb.gif","hideError"));


        JPanel south = new JPanel(new BorderLayout());
        south.setBorder(BorderFactory.createEtchedBorder());
        JXHeader header = new JXHeader(I18N.getMessage("headerTitle"),I18N.getMessage("headerDesc"),IconFactory.loadIcon("/resources/iconInfo.png"));
        header.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        header.setPreferredSize(new Dimension(320,60));
        final JTextArea textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(320,100));
        textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        textArea.setEditable(false);
        
        tree.setSelectionModel(new DefaultTreeSelectionModel()
        {
            @Override
            protected void fireValueChanged(TreeSelectionEvent e)
            {
                if(e.isAddedPath()){
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                    InfoBean bean = (InfoBean) node.getUserObject();
                    textArea.setText(bean.getComment());
                    
                }
                super.fireValueChanged(e);
            }

        });
        
        
        south.add(header,BorderLayout.NORTH);
        south.add(textArea,BorderLayout.CENTER);


        split.setLeftComponent(tree.getUI());
        split.setRightComponent(south);


//      mainFrame.add(tree.getUI(),BorderLayout.NORTH);
//      mainFrame.add(south,BorderLayout.CENTER);

        mainFrame.add(split);

        //TODO a voir
        mainFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        //mainFrame.setSize(300,300);
        mainFrame.pack();
    }


    public void setLocation(int x, int y)
    {
        mainFrame.setLocation(x, y);
    }

}
